#!/system/bin/sh

BASEDIR=/data/data/org.sshproxy
export PATH=$BASEDIR:$PATH

t=$1
host="$2"
username="$3"
passwd="$4"
port="$5"
global=$6

case $1 in
    start)
        plink -batch -l $username -pw $passwd -N -D 1984 -P $port $host < /dev/null > $BASEDIR/plink.log 2>&1 &
        sleep 5
        #pidof plink
        #if [ $? -ne 0 ];then
        #    echo "connect $host failed"
        #    exit 1
        #fi
        redsocks  -c $BASEDIR/redsocks.conf > /dev/null 2>&1 &

        polipo -c $BASEDIR/config > $BASEDIR/polipo.log 2>&1 &
        sleep 3
        if [ $global -ne 0 ];then
            su -c "sh $BASEDIR/firewall.sh start $host"
        fi
        exit 0
        ;;
    stop)
        killall plink
        killall redsocks
        killall polipo
        if [ $global -ne 0 ];then
            su -c "sh $BASEDIR/firewall.sh stop $host"
        fi
        exit 0
        ;;
esac
